package leetCode.solution3228;

/**
 * 超时解法
 */
public class Solution {

    public int maxOperations(String s) {
        if(s == null || s.isEmpty()) {
            return 0;
        }
        char[] chars = s.toCharArray();
        int num = 0;
        for(;;) {
            int index = getLeftIndex(chars);
            if(index == -1) {
                return num;
            }
            boolean result = rightMove(chars,index);
            if(result) num++;
        }
    }

    /**
     * 此方法用于找到从左到右的第一个符合条件的位置
     * @param s
     * @return
     */
    public int getLeftIndex(char[] sChars) {
        for (int i=0;i<sChars.length;i++) {
            if(i == sChars.length-1) {
                return -1;
            }
            if(sChars[i] == '1' && sChars[i+1] == '0' ) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 此方法用于将指定位置的1右移动到另一个1或末端
     * @param chars
     * @param index
     */
    public boolean rightMove(char[] chars,int index) {
        for(int i=index+1;i<chars.length;i++) {
            if(i == chars.length-1 && chars[i] == '0') {
                chars[index] = '0';
                chars[i] = '1';
                return true;
            }
            if(chars[i] == '0' && chars[i+1] == '1') {
                chars[index] = '0';
                chars[i] = '1';
                return true;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        long time = System.currentTimeMillis();
        System.out.println(solution.maxOperations("0100010101101001000011101011011001010000100110111100100111100000000011100011010011101110101011010110101110001011001111101100110000011011111101011011110100000100000011000010000110001100001100101101111011101010000111101001000111101011111110010100000101100011110101010101001100111110101010001001010001000001011100000001111110010000011001100110100000000010000100010110001110101001001000101010111010001001001111001000100100110111101000010011101111101010111101100111000110011111100000011011011100100111000011110000000111001011110100101111100110110111000000110110101110001110111101010110001001110110101010111110000110001001111111101000000001000000111001111001010001100010111100101110010001011001000101111110001101010111011001100000010001011001101111110101100101000111011011001110101101111000000000001011101011100111100010100101001111001000100100001100011111010100010111111101001110111000101110001001101011001011001101101101011101000111001010010101000100011000110001010001001011101010000101010010000001101011010111101111110010000111011100111011010011000110101111111010010111100111010111011101001100000000011110100011101110110100101111010011011100010100100100000100010010011100101010111000100001010010111000110111011010010010110111010110110001110100000000101010110011101001010101000101110011110111100100011010100110000010100101100000001101010011010000110110111001110110001001000111101101011000111101111000110010100100000110111101000111110000000011100100110000000111100011000101001100100000010001001100001100110110001010001010100001011011100001000101011101100101000110101111111100011110010101111111100100110000001111101001011110111011011001110010010010000111101100001010011011110000001101010011101000110110001110100011011000110110000101010000010000000010000101100001101110101000011010000111000001000010110110110010001110000101000001011110110101100010011101110010011101101100110110011110010000000110110010011101101011001011100001001011101101010100011010111001011111101111011000011100010001000111100100110010001101011111100100111110110110011101110010011110001110111010011011100101110011100001000110010111011110100010100110101110001110010000001001000000101011010011100111000001010011001011000111001100011110011000101111110101011110101000000010000101000110011101100000010111111110001110100101010010101111011000011010101111010110110011001111000011110001101000101100111100110000110110001000011101010001000110110101001110101101100000111001101000101110101000111011000101000100010010110000011001000011000010000000011110101011011100001100101111111001101111010011001110101011111001011011010101001001110110111001000011011100101100110100111110010110011010111111010100111001001000100011101010111101010101001001100001000111111000100110101111010111101000010110110000111101010001100011010011100001100110111000000001100111100000111110011000110001010100000011011011000100101101111011100011000111000011010001001100111010001101010100101111111000001100010001000011000000111110000000100001010111100100001000011100000011001100011101001011101000101011010011001111101000011101101111001100001001000010101111111011111101000011101110001001010101101001101011100010011010110111111101110000101110101010010001001111100000011001000110000100111001000101111001010110100111000010001011110101111001011101101000101000111111011011111010011000001110001000000000111111110001001000110011110111001011010000100111010110000011101000101010010100110001001101011110001101000111100101101011100101100100100100000111110000000010011101101110001110001101000111010100100111010111010011110010001000010101001001110101000111000101001011000000110110110010001001110101000011010010010111111011110111111110101111101101101010110000010110001101001001010111101111110001001010000001010100111100110010111000011101001001101110010001010111001101101000011011011001000110110000011110100101100011101000111001011010101000101110110100001111111100010111101010000001000111110101100100110110101110001011100111011001101011011010111110011110001000101011000111100011111001011110111001001110101101111011111000001101101000010010010011010010001111100101101101100001000111110000110110000100111110000110101101110100100001110011000110111000110001011010000011011011111010001011000111001000010010001001000000110100000001101001011101011100110101101100110100100000100111000100101100010101100001111111100111101110111101011000000001000110100001011110100011011110101000101011000000110100010100001010001100001011001101101100011111000101101011101110101101010101100000011000110101111111101110011011110110101110001100011011110111100000011011001110011100110011100111010001111101110111011100111110101111010101100101010000000000101111110100000101111100000101111011111001011010110110110000001110101100000010010110100111011000001000101110111011000100011110111100111011110110001111101110110011111110001111111100101110101100100011011011110010010101101100111000101000011001101001100100100110101111000110111000110011101110001101010111110010100100100011001011100100001101110000111000101010111000001010010111001111000111001010111110101110000011110101011110010001000101111110000001100001011111001010110001000101010110111000111110010100110110001010110101110100010011110010010010011000110111000000100011011110000110110110000011010010001111011011010001100111000100101000100110011001100110100110010101010110000110111001011010100011101101110001100111011110001010001100100000000000111011101101000110101101010011010111110011101010100111110000111101110101101100001000101100001100000011111011010001110101011111101011001101011001111011100101111011101000010000000100010100101101000110100010111011101011111101000111011111100011100011010100110011001100100100001101011100000001001010010011010100110010111100111001000101110110011101001111111010010110001010111011111010011010010110110000111100010110110100100011011001101101010111111101110001000110010001101111101101010111001011010010111010001000100000100101101111000111010111011000101001011110100010001010101110100110110101011011011000100001001111010100100000100011101001110111011100010010000001110011011101000010101101010100001111110100011011100100101011001101000110001011001011110000011000001011001100000001010001010110001101110111110001100111011000100000000111111111101001010100101111011001101100100100001111010000011101001001100100110100011001111010101010110010100110000111101000101011110111011010110111010101110000110100110101011000110100010001001011011001100001110110100100101100011011011101100101111101011001100101010000111011011101001111011000110101100011011010010001100010101110000010010001111011110011100010111110000101001101111000000110001100010001111110010000111110010000001101001111011010111101010001000011100011000001000011010111010100010001101111001001010010011110001110000000000010110111011111000111001010000000110010100111010011100101011101111011001110001110000111001010011110001100011011111011110111111100110110000100111110100011101110101001101000000101110011100110011111101100101101100100111011010010100101100101100010011001100111001010111111110000100110111111110000110000010110101110001000110101110100110011001111010001001011011111101011001110110100011110010010101101001101011110101101011010101111101001110111110011011000101011101001100"));
        System.out.println(System.currentTimeMillis()-time+"ms");
    }

}

//runtime:N/A
//memory:N/A
//total_testcases:694
//total_correct:658
//input_formatted:"0100010101101001000011101011011001010000100110111100100111100000000011100011010011101110101011010110101110001011001111101100110000011011111101011011110100000100000011000010000110001100001100101101111011101010000111101001000111101011111110010100000101100011110101010101001100111110101010001001010001000001011100000001111110010000011001100110100000000010000100010110001110101001001000101010111010001001001111001000100100110111101000010011101111101010111101100111000110011111100000011011011100100111000011110000000111001011110100101111100110110111000000110110101110001110111101010110001001110110101010111110000110001001111111101000000001000000111001111001010001100010111100101110010001011001000101111110001101010111011001100000010001011001101111110101100101000111011011001110101101111000000000001011101011100111100010100101001111001000100100001100011111010100010111111101001110111000101110001001101011001011001101101101011101000111001010010101000100011000110001010001001011101010000101010010000001101011010111101111110010000111011100111011010011000110101111111010010111100111010111011101001100000000011110100011101110110100101111010011011100010100100100000100010010011100101010111000100001010010111000110111011010010010110111010110110001110100000000101010110011101001010101000101110011110111100100011010100110000010100101100000001101010011010000110110111001110110001001000111101101011000111101111000110010100100000110111101000111110000000011100100110000000111100011000101001100100000010001001100001100110110001010001010100001011011100001000101011101100101000110101111111100011110010101111111100100110000001111101001011110111011011001110010010010000111101100001010011011110000001101010011101000110110001110100011011000110110000101010000010000000010000101100001101110101000011010000111000001000010110110110010001110000101000001011110110101100010011101110010011101101100110110011110010000000110110010011101101011001011100001001011101101010100011010111001011111101111011000011100010001000111100100110010001101011111100100111110110110011101110010011110001110111010011011100101110011100001000110010111011110100010100110101110001110010000001001000000101011010011100111000001010011001011000111001100011110011000101111110101011110101000000010000101000110011101100000010111111110001110100101010010101111011000011010101111010110110011001111000011110001101000101100111100110000110110001000011101010001000110110101001110101101100000111001101000101110101000111011000101000100010010110000011001000011000010000000011110101011011100001100101111111001101111010011001110101011111001011011010101001001110110111001000011011100101100110100111110010110011010111111010100111001001000100011101010111101010101001001100001000111111000100110101111010111101000010110110000111101010001100011010011100001100110111000000001100111100000111110011000110001010100000011011011000100101101111011100011000111000011010001001100111010001101010100101111111000001100010001000011000000111110000000100001010111100100001000011100000011001100011101001011101000101011010011001111101000011101101111001100001001000010101111111011111101000011101110001001010101101001101011100010011010110111111101110000101110101010010001001111100000011001000110000100111001000101111001010110100111000010001011110101111001011101101000101000111111011011111010011000001110001000000000111111110001001000110011110111001011010000100111010110000011101000101010010100110001001101011110001101000111100101101011100101100100100100000111110000000010011101101110001110001101000111010100100111010111010011110010001000010101001001110101000111000101001011000000110110110010001001110101000011010010010111111011110111111110101111101101101010110000010110001101001001010111101111110001001010000001010100111100110010111000011101001001101110010001010111001101101000011011011001000110110000011110100101100011101000111001011010101000101110110100001111111100010111101010000001000111110101100100110110101110001011100111011001101011011010111110011110001000101011000111100011111001011110111001001110101101111011111000001101101000010010010011010010001111100101101101100001000111110000110110000100111110000110101101110100100001110011000110111000110001011010000011011011111010001011000111001000010010001001000000110100000001101001011101011100110101101100110100100000100111000100101100010101100001111111100111101110111101011000000001000110100001011110100011011110101000101011000000110100010100001010001100001011001101101100011111000101101011101110101101010101100000011000110101111111101110011011110110101110001100011011110111100000011011001110011100110011100111010001111101110111011100111110101111010101100101010000000000101111110100000101111100000101111011111001011010110110110000001110101100000010010110100111011000001000101110111011000100011110111100111011110110001111101110110011111110001111111100101110101100100011011011110010010101101100111000101000011001101001100100100110101111000110111000110011101110001101010111110010100100100011001011100100001101110000111000101010111000001010010111001111000111001010111110101110000011110101011110010001000101111110000001100001011111001010110001000101010110111000111110010100110110001010110101110100010011110010010010011000110111000000100011011110000110110110000011010010001111011011010001100111000100101000100110011001100110100110010101010110000110111001011010100011101101110001100111011110001010001100100000000000111011101101000110101101010011010111110011101010100111110000111101110101101100001000101100001100000011111011010001110101011111101011001101011001111011100101111011101000010000000100010100101101000110100010111011101011111101000111011111100011100011010100110011001100100100001101011100000001001010010011010100110010111100111001000101110110011101001111111010010110001010111011111010011010010110110000111100010110110100100011011001101101010111111101110001000110010001101111101101010111001011010010111010001000100000100101101111000111010111011000101001011110100010001010101110100110110101011011011000100001001111010100100000100011101001110111011100010010000001110011011101000010101101010100001111110100011011100100101011001101000110001011001011110000011000001011001100000001010001010110001101110111110001100111011000100000000111111111101001010100101111011001101100100100001111010000011101001001100100110100011001111010101010110010100110000111101000101011110111011010110111010101110000110100110101011000110100010001001011011001100001110110100100101100011011011101100101111101011001100101010000111011011101001111011000110101100011011010010001100010101110000010010001111011110011100010111110000101001101111000000110001100010001111110010000111110010000001101001111011010111101010001000011100011000001000011010111010100010001101111001001010010011110001110000000000010110111011111000111001010000000110010100111010011100101011101111011001110001110000111001010011110001100011011111011110111111100110110000100111110100011101110101001101000000101110011100110011111101100101101100100111011010010100101100101100010011001100111001010111111110000100110111111110000110000010110101110001000110101110100110011001111010001001011011111101011001110110100011110010010101101001101011110101101011010101111101001110111110011011000101011101001100"
//expected_output:3250132
//code_output:
//runtime_error:
//last_testcase:"0100010101101001000011101011011001010000100110111100100111100000000011100011010011101110101011010110101110001011001111101100110000011011111101011011110100000100000011000010000110001100001100101101111011101010000111101001000111101011111110010100000101100011110101010101001100111110101010001001010001000001011100000001111110010000011001100110100000000010000100010110001110101001001000101010111010001001001111001000100100110111101000010011101111101010111101100111000110011111100000011011011100100111000011110000000111001011110100101111100110110111000000110110101110001110111101010110001001110110101010111110000110001001111111101000000001000000111001111001010001100010111100101110010001011001000101111110001101010111011001100000010001011001101111110101100101000111011011001110101101111000000000001011101011100111100010100101001111001000100100001100011111010100010111111101001110111000101110001001101011001011001101101101011101000111001010010101000100011000110001010001001011101010000101010010000001101011010111101111110010000111011100111011010011000110101111111010010111100111010111011101001100000000011110100011101110110100101111010011011100010100100100000100010010011100101010111000100001010010111000110111011010010010110111010110110001110100000000101010110011101001010101000101110011110111100100011010100110000010100101100000001101010011010000110110111001110110001001000111101101011000111101111000110010100100000110111101000111110000000011100100110000000111100011000101001100100000010001001100001100110110001010001010100001011011100001000101011101100101000110101111111100011110010101111111100100110000001111101001011110111011011001110010010010000111101100001010011011110000001101010011101000110110001110100011011000110110000101010000010000000010000101100001101110101000011010000111000001000010110110110010001110000101000001011110110101100010011101110010011101101100110110011110010000000110110010011101101011001011100001001011101101010100011010111001011111101111011000011100010001000111100100110010001101011111100100111110110110011101110010011110001110111010011011100101110011100001000110010111011110100010100110101110001110010000001001000000101011010011100111000001010011001011000111001100011110011000101111110101011110101000000010000101000110011101100000010111111110001110100101010010101111011000011010101111010110110011001111000011110001101000101100111100110000110110001000011101010001000110110101001110101101100000111001101000101110101000111011000101000100010010110000011001000011000010000000011110101011011100001100101111111001101111010011001110101011111001011011010101001001110110111001000011011100101100110100111110010110011010111111010100111001001000100011101010111101010101001001100001000111111000100110101111010111101000010110110000111101010001100011010011100001100110111000000001100111100000111110011000110001010100000011011011000100101101111011100011000111000011010001001100111010001101010100101111111000001100010001000011000000111110000000100001010111100100001000011100000011001100011101001011101000101011010011001111101000011101101111001100001001000010101111111011111101000011101110001001010101101001101011100010011010110111111101110000101110101010010001001111100000011001000110000100111001000101111001010110100111000010001011110101111001011101101000101000111111011011111010011000001110001000000000111111110001001000110011110111001011010000100111010110000011101000101010010100110001001101011110001101000111100101101011100101100100100100000111110000000010011101101110001110001101000111010100100111010111010011110010001000010101001001110101000111000101001011000000110110110010001001110101000011010010010111111011110111111110101111101101101010110000010110001101001001010111101111110001001010000001010100111100110010111000011101001001101110010001010111001101101000011011011001000110110000011110100101100011101000111001011010101000101110110100001111111100010111101010000001000111110101100100110110101110001011100111011001101011011010111110011110001000101011000111100011111001011110111001001110101101111011111000001101101000010010010011010010001111100101101101100001000111110000110110000100111110000110101101110100100001110011000110111000110001011010000011011011111010001011000111001000010010001001000000110100000001101001011101011100110101101100110100100000100111000100101100010101100001111111100111101110111101011000000001000110100001011110100011011110101000101011000000110100010100001010001100001011001101101100011111000101101011101110101101010101100000011000110101111111101110011011110110101110001100011011110111100000011011001110011100110011100111010001111101110111011100111110101111010101100101010000000000101111110100000101111100000101111011111001011010110110110000001110101100000010010110100111011000001000101110111011000100011110111100111011110110001111101110110011111110001111111100101110101100100011011011110010010101101100111000101000011001101001100100100110101111000110111000110011101110001101010111110010100100100011001011100100001101110000111000101010111000001010010111001111000111001010111110101110000011110101011110010001000101111110000001100001011111001010110001000101010110111000111110010100110110001010110101110100010011110010010010011000110111000000100011011110000110110110000011010010001111011011010001100111000100101000100110011001100110100110010101010110000110111001011010100011101101110001100111011110001010001100100000000000111011101101000110101101010011010111110011101010100111110000111101110101101100001000101100001100000011111011010001110101011111101011001101011001111011100101111011101000010000000100010100101101000110100010111011101011111101000111011111100011100011010100110011001100100100001101011100000001001010010011010100110010111100111001000101110110011101001111111010010110001010111011111010011010010110110000111100010110110100100011011001101101010111111101110001000110010001101111101101010111001011010010111010001000100000100101101111000111010111011000101001011110100010001010101110100110110101011011011000100001001111010100100000100011101001110111011100010010000001110011011101000010101101010100001111110100011011100100101011001101000110001011001011110000011000001011001100000001010001010110001101110111110001100111011000100000000111111111101001010100101111011001101100100100001111010000011101001001100100110100011001111010101010110010100110000111101000101011110111011010110111010101110000110100110101011000110100010001001011011001100001110110100100101100011011011101100101111101011001100101010000111011011101001111011000110101100011011010010001100010101110000010010001111011110011100010111110000101001101111000000110001100010001111110010000111110010000001101001111011010111101010001000011100011000001000011010111010100010001101111001001010010011110001110000000000010110111011111000111001010000000110010100111010011100101011101111011001110001110000111001010011110001100011011111011110111111100110110000100111110100011101110101001101000000101110011100110011111101100101101100100111011010010100101100101100010011001100111001010111111110000100110111111110000110000010110101110001000110101110100110011001111010001001011011111101011001110110100011110010010101101001101011110101101011010101111101001110111110011011000101011101001100"

